package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.Listener.SujectExcelListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author lanhaif
 * @since 2022-08-22
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

//    添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            InputStream in= file.getInputStream();
            EasyExcel.read(in,SubjectData.class,new SujectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20002,"添加课表失败");
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        try {
//            1.查询一级分类数据
            QueryWrapper<EduSubject> wrapper1=new QueryWrapper<>();
            wrapper1.eq("parent_id","0");
//            List<EduSubject> oneSubjects = baseMapper.selectList(wrapper);
            List<EduSubject> oneSubjects=this.list(wrapper1);
//              2.查询二级数据
            QueryWrapper<EduSubject> wrapper2=new QueryWrapper<>();
            wrapper2.ne("parent_id","0");
            List<EduSubject> twoSubjects=this.list(wrapper2);

            List<OneSubject> finalSujectList=new ArrayList<>();

//            3.封装一级分类
            for (int i = 0; i < oneSubjects.size(); i++) {
                EduSubject eduSubject = oneSubjects.get(i);
                OneSubject oneSubject=new OneSubject();
//                方法一
//                oneSubject.setId(eduSubject.getId());
//                oneSubject.setTitle(eduSubject.getTitle());
//                  方法二
                BeanUtils.copyProperties(eduSubject,oneSubject);
                finalSujectList.add(oneSubject);

                List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
                //            4.封装二级分类
                for (int j = 0; j < twoSubjects.size(); j++) {
                    EduSubject tSubject = twoSubjects.get(j);
//                    判断二级匪类parent_id是否和一级分类id一样
                    if (tSubject.getParentId().equals(eduSubject.getId()) ) {
                        TwoSubject twoSubject=new TwoSubject();
                        BeanUtils.copyProperties(tSubject,twoSubject);
                        twoFinalSubjectList.add(twoSubject);
                    }
                }
                oneSubject.setChildren(twoFinalSubjectList);
            }
            return finalSujectList;

        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20002,"课程列表返回失败");
        }

    }
}
